<template>
  <div class="sex">
    <div class="man">
      <div class="sex-xb">男士</div>
      <img src="../../images/man.png" alt="" />
    </div>
    <div class="women">
      <div class="sex-xb">女士</div>
      <img src="../../images/woman.png" alt="" />
    </div>
  </div>
  <div class="charts-box">
    <div class="charts" ref="charts"></div>
  </div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import * as echarts from 'echarts'
interface ChartProp {
  man: number
  woman: number
}
//获取图形图标的DOM节点
let charts = ref()
let data: ChartProp = {
  man: 0.6,
  woman: 0.4,
}
onMounted(() => {
  //初始化echarts实例
  let mycharts = echarts.init(charts.value)
  mycharts.setOption({
    grid: {
      left: 0,
      top: 0,
      bottom: 0,
      right: 0,
    },
    xAxis: {
      show: false,
      type: 'value',
    },
    yAxis: [
      {
        type: 'category',
        position: 'left',
        data: ['男生'],
        axisTick: {
          show: false,
        },
        axisLine: {
          show: false,
        },
        axisLabel: {
          show: false,
        },
      },
      {
        type: 'category',
        position: 'right',
        data: ['女士'],
        axisTick: {
          show: false,
        },
        axisLine: {
          show: false,
        },
        axisLabel: {
          show: false,
          padding: [0, 0, 40, -60],
          fontSize: 12,
          lineHeight: 60,
          color: 'rgba(255, 255, 255, 0.9)',
          formatter: '{value}' + data.woman * 100 + '%',
          rich: {
            a: {
              color: 'transparent',
              lineHeight: 30,
              fontFamily: 'digital',
              fontSize: 12,
            },
          },
        },
      },
    ],
    series: [
      {
        type: 'bar',
        data: [data.man],
        barWidth: 20,
        z: 20,
        itemStyle: {
          borderRadius: 10,
          color: '#007AFE',
        },
        label: {
          show: true,
          position: 'insideLeft',
          offset: [0, -20],
          formatter: () => {
            return `男士 ${data.man * 100}%`
          },
        },
      },
      {
        type: 'bar',
        data: [1],
        barWidth: 20,
        barGap: '-100%',
        itemStyle: {
          borderRadius: 10,
          color: '#FF4B7A',
        },
        label: {
          show: true,
          color: '#E7E8ED',
          position: 'insideRight',
          fontSize: 12,
          offset: [0, -20],
          formatter: () => {
            return `女士 ${data.woman * 100}%`
          },
        },
      },
    ],
  })
})
</script>
<style lang="scss" scoped>
.sex {
  box-sizing: border-box;
  padding-top: 20px;
  width: 100%;
  display: flex;
  justify-content: center;
  position: relative;
  .sex-xb {
    color: white;
    position: absolute;
    top: 2px;
  }
  .man {
    position: relative;
    width: 111px;
    height: 115px;
    background: url(../../images/man-bg.png) no-repeat;
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
  }

  .women {
    margin: 0 0 0 40px;
    position: relative;
    width: 111px;
    height: 115px;
    background: url(../../images/woman-bg.png) no-repeat;
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
  }
}
.charts-box {
  width: 100%;
  height: 100%;
  padding: 0 60px;
}
.charts {
  width: 100%;
  height: calc(100% - 135px);
}
</style>
